<?php

namespace falcon5\inc\Http\Controllers;

use falcon5\inc\Http\JsonResponse;
use falcon5\inc\Http\Request;
use Monolog\Logger;

/**
 * Class LoggerController
 * @package falcon5\inc\Http\Controllers
 */
class LoggerController extends BaseController
{
    /**
     * 保存错误日志
     * @param Request $request
     * @return JsonResponse
     */
    public function saveLog(Request $request): JsonResponse
    {
        try {
            // 获取并解码错误数据
            $errorData = $request->get('error_data');
            if (empty($errorData)) {
                return JsonResponse::error(400, 'error data is empty');
            }

            $errorData = json_decode(stripslashes($errorData), true);
            if (json_last_error() !== JSON_ERROR_NONE) {
                return JsonResponse::error(400, 'error data format is incorrect');
            }

            // 获取Monolog实例
            /** @var Logger $logger */
            $logger = falcon5_app('monolog');

            // 提取日志信息
            $message = $errorData['msg'] ?? 'Unknown error';
            $level = strtoupper($errorData['level'] ?? 'error');
            $context = [
                'error_type' => $errorData['request_params']['error_type'] ?? 'unknown',
                'filename' => $errorData['request_params']['filename'] ?? '',
                'lineno' => $errorData['request_params']['lineno'] ?? 0,
                'colno' => $errorData['request_params']['colno'] ?? 0,
                'stack' => $errorData['context']['stack'] ?? '',
            ];
            
            // 添加额外信息
            $extra = [
                'browser_info' => $errorData['extra']['browser_info'] ?? [],
                'request_url' => $errorData['request_params']['url'] ?? '',
                'user_agent' => $errorData['request_params']['user_agent'] ?? '',
                'trace_id' => $errorData['trace_id'] ?? '',
                'uri' => $errorData['uri'] ?? '',
                'module' => $errorData['module'] ?? 'falcon5-js',
            ];

            // 记录日志
            switch ($level) {
                case 'DEBUG':
                    $logger->debug($message, $context);
                    break;
                case 'INFO':
                    $logger->info($message, $context);
                    break;
                case 'NOTICE':
                    $logger->notice($message, $context);
                    break;
                case 'WARNING':
                    $logger->warning($message, $context);
                    break;
                case 'ERROR':
                    $logger->error($message, $context);
                    break;
                case 'CRITICAL':
                    $logger->critical($message, $context);
                    break;
                case 'ALERT':
                    $logger->alert($message, $context);
                    break;
                case 'EMERGENCY':
                    $logger->emergency($message, $context);
                    break;
                default:
                    $logger->error($message, $context);
            }

            return JsonResponse::success([
                'status' => 'success',
                'message' => 'log saved'
            ]);

        } catch (\Throwable $e) {
            // 记录处理错误日志时的错误
            falcon5_error_log('Error processing log: ' . $e->getMessage());
            return JsonResponse::error(500, 'log processing failed: ' . $e->getMessage());
        }
    }
}